package com.example.mpandroidchart_all.FragmentChart;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.mpandroidchart_all.R;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.utils.Utils;

import java.util.ArrayList;
import java.util.List;

public class FragmentPieChart extends Fragment {
    private PieChart pieChart;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_piechart,container,false);
        pieChart=view.findViewById(R.id.pie_chart);

        setDescription();
        setLegend();
        initData();

        return view;
    }
    private void initData() {
        pieChart.setRotationEnabled(false);
        pieChart.setDrawEntryLabels(false);
        pieChart.setUsePercentValues(true);
        pieChart.setDrawHoleEnabled(false);
        pieChart.setExtraOffsets(0f,30f,0f,25f);
        setChartData();
    }
    private void setChartData(){
        List<PieEntry> value1=new ArrayList<>();
        value1.add(new PieEntry(28.6f, "有违章"));
        value1.add(new PieEntry(71.4f, "无违章"));

        List<Integer> colors = new ArrayList<>();
        colors.add(Color.parseColor("#4A92FC"));
        colors.add(Color.parseColor("#ee6e55"));

        PieDataSet set1=new PieDataSet(value1,"");
        set1.setColors(colors);
        set1.setSliceSpace(2f);
        set1.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        final String [] strs={"28.6%","71.4%"};
        set1.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                Log.e("MainActivity","value="+value);
                String flag="";
                if((int) value%2==0){
                    flag="有违章：";
                }else{
                    flag="无违章：";
                }
                return flag+strs[(int) value%2];
            }
        });
        PieData pieData=new PieData(set1);
        pieChart.setData(pieData);
        pieChart.invalidate();
    }
    private void setDescription(){
        Description description=new Description();
        String descriptionStr="平台上有违章车辆和没违章车辆的占比统计";
        description.setText(descriptionStr);
        description.setTextSize(18f);
        description.setTextColor(Color.parseColor("#4A92FC"));

        //获取屏幕中间x轴的像素坐标
        WindowManager wm= (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm=new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        float x=dm.widthPixels/2;

        Paint paint=new Paint();
        paint.setTextSize(18f);
        float height= Utils.calcTextHeight(paint,descriptionStr);
        float y=Utils.convertDpToPixel(height+12);
        description.setPosition(x,y);
        description.setTextAlign(Paint.Align.CENTER);
        pieChart.setDescription(description);
    }
    private void setLegend(){
        Legend legend = pieChart.getLegend();
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        legend.setDrawInside(true);
    }
}
